package sandbox;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

import org.joda.time.Duration;
import java.util.Set;

public final class TaskSet {
	private static volatile Set<Task> tasks = null;

	public static void add(Task[] tasks) {
		if (tasks == null)
			return;
		if (TaskSet.tasks == null) {
			synchronized (TaskSet.class) {
				if (TaskSet.tasks == null)
					TaskSet.tasks = new HashSet<Task>();
			}
		}

		for (Task task : tasks)
			TaskSet.tasks.add(task);
	}

	public static Set<Task> get() throws Exception {
		if (TaskSet.tasks == null)
			throw new Exception("WorkerSet.workers == null");
		return TaskSet.tasks;
	}

	public static List<Task> get(Comparator<Task> comparator) throws Exception {
		List<Task> taskList = new ArrayList<TaskSet.Task>(get());
		Collections.sort(taskList, comparator);
		return taskList;
	}

	public class Task extends Base {
		private int profit;
		private int cost;
		private Duration duration;

		public int getProfit() {
			return profit;
		}

		public int getCost() {
			return cost;
		}

		public Duration getDuration() {
			return duration;
		}

		public Task(int profit, int cost, Duration duration) {
			this.profit = profit;
			this.cost = cost;
			this.duration = duration;
		}

		@Override
		public String toString() {
			return super.toString() + ", profit: " + profit + ", cost: " + cost
					+ ", duration: " + duration.getMillis();
		}
	}
}